package cn.action.photo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import cn.action.BaseAction;
import cn.beans.Photo;
import cn.beans.Product;
import cn.beans.ProductFile;
import cn.utils.DateFormat;

import com.opensymphony.xwork2.ActionContext;

public class PhotoAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File[] pic;
	private String[] picFileName;
	private String[] picContentType;
	private String errorinfo;
	private List listPhoto;
	private String photoId;
	private int totlepic;
	private String id;
	private String keyword;
	
	
	public String ListPhoto(){
		String hql = "SELECT p.id,p.saveName,p.keyword,p.name FROM Photo p  ORDER BY p.creDate desc";
		listPhoto = findSomeByHQL.findDataByHql(hql, null);
				return SUCCESS;
		
	}
	
	public String indexListPhoto(){
		String hql = "FROM 	Photo p  ORDER BY p.creDate desc";
		List list = findSomeByHQL.findDataByHql(hql, null);
			if(null != list && !list.isEmpty()){
				listPhoto = new ArrayList();
				int size = list.size();
				for(int i=0; i<size; i++){
					Photo photo = (Photo)list.get(i);
					if(null != photo){
						listPhoto.add(photo);
					}
				}
				totlepic = list.size();
				return SUCCESS;
			}else{
				return SUCCESS;
			}
		
	}
	/**
	 * 上传图片
	 * @return
	 */
	public String uploadPhoto() throws Exception{
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		String sadminName = (String)ActionContext.getContext().getSession().get("adminName");
		if(null == sadminId){
			errorinfo = "Time over";
			return LOGIN;
		}else{
			if(null != pic){
				int length = pic.length;
				for(int i=0;i<length;i++){
					if(null != pic[i]){
						InputStream is = new FileInputStream(pic[i]);
						int pos = picFileName[i].lastIndexOf(".");
						String extName = picFileName[i].substring(pos).toLowerCase();
						if(!(".jpg".equals(extName) || ".gif".equals(extName) 
							|| ".jpeg".equals(extName) || ".png".equals(extName))){
							
							errorinfo = "picType";
							return INPUT;
						}else{
							String uploadFilename = DateFormat.getDateId() + extName;
							String root = ServletActionContext.getServletContext()
									.getRealPath("/upload/indexshow");
							String path = root+"/"+uploadFilename;
							File app = new File(root);
							if (!app.exists()) {
								app.mkdir();
							}
							File dest = new File(root, uploadFilename);

							OutputStream os;
							os = new FileOutputStream(dest);
							byte[] buffer = new byte[1024];
							int len = 0;
							int total = 0;
							while ((len = is.read(buffer)) > 0) {
								total += len;
								os.write(buffer, 0, len);
							}
							is.close();
							os.close();
							
							Photo photo = new Photo();
							photo.setAdminId(sadminId);
							photo.setAdminName(sadminName);
							photo.setCreDate(new Date());
							photo.setExtent(extName);
							photo.setName(picFileName[i]);
							photo.setSaveName(uploadFilename);
							photo.setSize(total);
							photo.setPath(path);
							commonservice.saveDemo(photo);
						}
					}
				}
			}
		}
		return SUCCESS;
	}
	
	
	/**
	 * 删除产品展示图片
	 * @throws Exception
	 */
	public void deletePhoto() throws Exception{
		try{
			String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == sadminId){
				this.sendMsgAjax("login", null);
			}else{
				Photo photo = commonservice.findById(photoId, new Photo());
				String productPath = ServletActionContext.getServletContext().getRealPath("/upload/indexshow");
				String file = productPath + "\\" + photo.getSaveName();
				File f = new File(file);
				if(f.exists()){
					f.delete();
				}
				commonservice.deleteDemo(photo, null);
				this.sendMsgAjax("success", null);
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 删除广告图片
	 * @throws Exception
	 */
	public String deletePhotonow(){
			String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == sadminId){
				errorinfo = "Time over";
				return LOGIN;
			}else{
				Photo photo = commonservice.findById(id, new Photo());
				String productPath = ServletActionContext.getServletContext().getRealPath("/upload/indexshow");
				String file = productPath + "\\" + photo.getSaveName();
				File f = new File(file);
				if(f.exists()){
					f.delete();
				}
				commonservice.deleteDemo(photo, null);
				String hql = "SELECT p.id,p.saveName,p.keyword,p.name FROM Photo p  ORDER BY p.creDate desc";
				listPhoto = findSomeByHQL.findDataByHql(hql, null);
				return SUCCESS;
			}
	}
	
	/**
	 * 新的上传图片
	 * @return
	 */
	public String uploadpic() throws Exception{
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		String sadminName = (String)ActionContext.getContext().getSession().get("adminName");
		if(null == sadminId){
			errorinfo = "Time over";
			return LOGIN;
		}else{
			if(null != pic){
				int length = pic.length;
				for(int i=0;i<length;i++){
					if(null != pic[i]){
						InputStream is = new FileInputStream(pic[i]);
						int pos = picFileName[i].lastIndexOf(".");
						String extName = picFileName[i].substring(pos).toLowerCase();
						if(!(".jpg".equals(extName))){
							errorinfo = "只能上传.jpg图片，请重新选择……";
							String hql = "SELECT p.id,p.saveName,p.keyword,p.name FROM Photo p  ORDER BY p.creDate desc";
							listPhoto = findSomeByHQL.findDataByHql(hql, null);
							return "error";
						}else{
							String uploadFilename = DateFormat.getDateId() + extName;
							String root = ServletActionContext.getServletContext()
									.getRealPath("/upload/indexshow");
							String path = root+"/"+uploadFilename;
							File app = new File(root);
							if (!app.exists()) {
								app.mkdir();
							}
							File dest = new File(root, uploadFilename);

							OutputStream os;
							os = new FileOutputStream(dest);
							byte[] buffer = new byte[1024];
							int len = 0;
							int total = 0;
							while ((len = is.read(buffer)) > 0) {
								total += len;
								os.write(buffer, 0, len);
							}
							is.close();
							os.close();
							
							Photo photo = new Photo();
							photo.setAdminId(sadminId);
							photo.setAdminName(sadminName);
							photo.setCreDate(new Date());
							photo.setExtent(extName);
							photo.setName(picFileName[i]);
							photo.setSaveName(uploadFilename);
							photo.setSize(total);
							photo.setPath(path);
							photo.setKeyword(keyword);
							commonservice.saveDemo(photo);
						}
					}
				}
			}
		}
		return SUCCESS;
	}
	
	public File[] getPic() {
		return pic;
	}
	public void setPic(File[] pic) {
		this.pic = pic;
	}
	public String[] getPicFileName() {
		return picFileName;
	}
	public void setPicFileName(String[] picFileName) {
		this.picFileName = picFileName;
	}
	public String[] getPicContentType() {
		return picContentType;
	}
	public void setPicContentType(String[] picContentType) {
		this.picContentType = picContentType;
	}
	public String getErrorinfo() {
		return errorinfo;
	}
	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}
	public List getListPhoto() {
		return listPhoto;
	}
	public void setListPhoto(List listPhoto) {
		this.listPhoto = listPhoto;
	}
	public String getPhotoId() {
		return photoId;
	}
	public void setPhotoId(String photoId) {
		this.photoId = photoId;
	}
	public int getTotlepic() {
		return totlepic;
	}
	public void setTotlepic(int totlepic) {
		this.totlepic = totlepic;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
}
